<template>
    <van-nav-bar
        title="编辑收货地址"
        left-text="返回"
        left-arrow
        @click-left="back"
    />

    <van-address-edit
        :address-info="info"
        :area-list="areaList"
        show-delete
        show-set-default
        :area-columns-placeholder="['请选择', '请选择', '请选择']"
        @save="save"
        @delete="del"
    />
    
</template>

<script setup>
    import {areaList} from '@vant/area-data'
    import {getCurrentInstance} from 'vue'
    const {proxy} = getCurrentInstance()

    var business = reactive(proxy.$cookies.get('business'))

    // 收获地址的默认信息
    var info = ref({})

    // 获取参数
    let id = proxy.$route.query.id ? proxy.$route.query.id : 0
    id = ref(id)

    onBeforeMount(() => {
        Address()
    })

    const back = () => {
        proxy.$router.go(-1)
        return false
    }
    
    let Address = async () => {
        // 组装数据
        var data = {
            id: id.value,
            busid: business.id
        }
        // console.log(data);
        // return false
        var result = await proxy.$POST({
            url:'/address/info',
            params: data
        })

        if(result.code == 0){
            proxy.$fail(result.msg)
            proxy.$router.go(-1)
            return false
        }

        var data = result.data
        var code = data.district || data.city || data.province

        info.value = {
            name: data.consignee,
            tel: data.mobile,
            areaCode: code,
            addressDetail: data.address,
            isDefault: data.status == '1' ? true : false
        }
    }

    let save = async (info) => {
        // 组装数据
        var data = {
            id: id.value,
            busid: business.id,
            consignee: info.name,
            code: info.areaCode,
            address: info.addressDetail,
            mobile: info.tel,
            status: info.isDefault ? '1' : '0'
        }

        var result = await proxy.$POST({
            url: '/address/edit',
            params: data
        })

        if(result.code == 0){
            proxy.$fail(result.msg)
            return false
        }else{
            proxy.$success(result.msg)
            proxy.$router.go(-1) 
        }
    } 

    let del = async (info) => {
        // 组装数据
        var data = {
            id: id.value,
            busid: business.id 
        }

        // 提示
        proxy.$confirm({
            title:'删除提示',
            message: '是否删除该收货地址'
        }).then(async () => {
            // 发请求
            var result = await proxy.$POST({
                url: '/address/del',
                params: data  
            })

            if(result.code == 0){
                proxy.$fail('删除失败')
                return false
            }else{
                proxy.$success('删除成功')
                proxy.$router.go(-1);
            } 
        }).catch(()=>{})
    }
    
</script>